#include "xmlelementiterator.h"
#include "xmlelement.h"

namespace MCCommon
{

XMLElementIterator::XMLElementIterator
(
    xercesc::DOMNodeIterator* iter,
    xercesc::DOMNodeFilter* filter /*= 0*/
)
:   fIter(iter),
    fCurNode(0),
    fFilter(filter)
{
    fAccCount.Increase();
}

XMLElementIterator::XMLElementIterator
(
    const XMLElementIterator& eleIter
)
{
    if ( fAccCount.GetCount() == 1) 
    {
        release();
    }

    fAccCount.Decrease();
    fIter = eleIter.fIter;
    fCurNode = eleIter.fCurNode;
    fFilter = eleIter.fFilter;
    fAccCount = eleIter.fAccCount;	
    fAccCount.Increase();
}

XMLElementIterator& 
XMLElementIterator::operator = 
(
    const XMLElementIterator& eleIter
)
{
    XMLElementIterator::XMLElementIterator(eleIter);
    
    return *this;
}

XMLElementIterator::~XMLElementIterator()
{
    if (fAccCount.GetCount() == 1) 
    {
        release();		
    }
    fAccCount.Decrease();
}

bool 
XMLElementIterator::HasMore()
{
    if(fIter == 0)
        return false;

    fCurNode = fIter->nextNode();
    if(fCurNode == 0)
    {
        return false;
    }
    else
    {
        return true;
    }
}

XMLElement 
XMLElementIterator::Next()
{
    xercesc::DOMElement *pDomEle = 0;
    if( fCurNode != 0 && 
        fCurNode->getNodeType() == xercesc::DOMNode::ELEMENT_NODE )
    {
        pDomEle = (xercesc::DOMElement*)fCurNode;  
    }

    return XMLElement(pDomEle);    
}

void XMLElementIterator::release()
{
    if(fIter != 0)
    {
        fIter->release();
    }

    if(fFilter != 0)
    {
        delete fFilter;
    }
}

} // end of namespace MCXML_NAMESPACE.